<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
<!--        <fieldset class="table-search-fieldset">-->
<!--            <legend>搜索信息</legend>-->
<!--            <div style="margin: 10px 10px 10px 10px">-->
<!--                <form class="layui-form layui-form-pane" action="">-->
<!--                    <div class="layui-form-item">-->
<!--                        <div class="layui-inline">-->
<!--                            <label class="layui-form-label">姓名</label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="text" name="name" autocomplete="off" class="layui-input">-->
<!--                            </div>-->
<!--                        </div>-->
<!--                        <div class="layui-inline">-->
<!--                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </form>-->
<!--            </div>-->
<!--        </fieldset>-->

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal  layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i> 添加 </button>
                <button class="layui-btn layui-btn-sm  layui-btn-danger data-delete-btn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i> 删除 </button>
                <button class="layui-btn layui-btn-sm  data-delete-btn" lay-event="update"><i class="layui-icon layui-icon-edit"></i> 修改 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'axios', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            axios = layui.axios,
            layer = layui.layer,
            miniPage = layui.miniPage;

        //渲染表格
        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: 'notice/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', title: 'ID'},
                {field: 'title', title: '标题', width: 150},
                {field: 'contend', title: '内容', width: 300},
                {field: 'createDate', width: 200, title: '创建日期'},
                {field: 'validity', width: 80, title: '有效期', templet: function (data) {
                        return data.validity + " 天";
                    }},
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('./notice-add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加公告',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length <= 0){
                    layer.msg("请选择至少一条记录进行删除");
                }else{
                    //获取选中数据中的id项构成数组
                    let arr = data.map(item=>item.id);

                    //异步请求删除
                    axios({
                        method: 'get',
                        url: 'notice/delete?ids=' + arr.join(",") //将数组中的项按,分隔拼接成字符串
                    }).then(response => {
                        layer.msg(response.msg, function (){
                            layui.table.reload("currentTableId");
                        })
                    }).catch(error => {
                        layer.msg(error);
                    });
                }
            } else if (obj.event === 'update') {  // 监听修改操作
                //获得选中的数据
                let data = table.checkStatus('currentTableId').data;
                if(data.length == 1){
                    var content = miniPage.getHrefContent('./notice-edit.html');
                    var openWH = miniPage.getOpenWidthHeight();

                    var index = layer.open({
                        title: '修改公告信息',
                        type: 1,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: [openWH[0] + 'px', openWH[1] + 'px'],
                        offset: [openWH[2] + 'px', openWH[3] + 'px'],
                        content: content,
                    });

                    console.log(data[0])
                    console.log(data[0])
                    console.log(data[0])
                    //给表单赋值
                    form.val("editForm", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                        "id": data[0].id // "name": "value"
                        ,"title": data[0].title
                        ,"contend": data[0].contend
                        ,"createDate": data[0].createDate
                        ,"validity": data[0].validity
                    });
                }else{
                    layer.msg("请选择一条数据进行修改")
                }


                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

    });
</script>